//
//  ThirdListViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/12.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class ThirdListViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
    var ThirdListArray = [ThirdListModel]()
    
    var bigThirdListArray = [ThirdListModel]()
    var collection :UICollectionView!
    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    var index = 1
    
    
    
    var ImageViewArray : [String] = ["app_hot_rec_1","app_hot_rec_2","app_hot_rec_1","app_hot_rec_2","app_hot_rec_4","app_hot_rec_3","app_hot_rec_1","app_hot_rec_2"]
    override func viewDidLoad() {
        super.viewDidLoad()
   
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width:(self.view.frame.size.width - 20)/2,height:130)
        //列间距,行间距,偏移
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5)
        
        
        if UIScreen.main.bounds.size.height == 812 {
            collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 44 - 83  - 64 - 20) , collectionViewLayout: layout)
        }else{
            collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height -  64 - 49) , collectionViewLayout: layout)
        }
        
        
        
        
        collection.backgroundColor = UIColor.white
        //        collection.register(FallCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "Cellid")
        
        collection.register(ThirdListCollectionViewCell.self, forCellWithReuseIdentifier: "Cellid")
        
        
        collection.delegate = self
        collection.dataSource = self
        self.view.addSubview(collection)
        

        
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        // 现在的版本要用mj_header
        collection.mj_header = header

         collection.mj_header.beginRefreshing()

        // 上拉刷新
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        collection.mj_footer = footer
        
    }
    
    @objc func footerRefresh(){
        
        self.ThirdListArray.removeAll()
       
        index += 1
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(17)\",\"start\":\"\(index)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        print("重点产业====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("333333====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = ThirdListModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    model1.articlePic = dataDic["articlePic"].string
                    
                    self.ThirdListArray.append(model1)
                    
                    
                    
                }
                
                self.bigThirdListArray.append(contentsOf: self.ThirdListArray)
                
                self.collection .reloadData()
                
                self.collection.mj_footer.endRefreshing()
                
                
            }else{
                
                print("数据请求失败")
            }
        }
        
    }
    
    @objc func headerRefresh(){
        
        self.ThirdListArray.removeAll()
        self.bigThirdListArray.removeAll()
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(17)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("333333====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = ThirdListModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    model1.articlePic = dataDic["articlePic"].string
                    
                    self.ThirdListArray.append(model1)
                    
                    
                    
                }
                
                self.bigThirdListArray.append(contentsOf: self.ThirdListArray)
                
                self.collection .reloadData()
                
                self.collection.mj_header.endRefreshing()
                
                
            }else{
                
                print("数据请求失败")
            }
        }
        
    }
    
   
   
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.bigThirdListArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
         let thirdListModel:ThirdListModel = self.bigThirdListArray[indexPath.row]

        
        
        let cell : ThirdListCollectionViewCell = collection.dequeueReusableCell(withReuseIdentifier: "Cellid", for: indexPath) as! ThirdListCollectionViewCell
        
 
        
          cell.thirdListModelWithModel(model: thirdListModel)
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        print(indexPath.row)
        
        let thirdListModel:ThirdListModel = self.bigThirdListArray[indexPath.row]
        
        let WebVC =  WebViewController()
        WebVC.numInteger = thirdListModel.id
        self.navigationController?.pushViewController(WebVC , animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    


}

